package com.dmxh.service.impl;

import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.dmxh.mapper.TbBrandMapper;
import com.dmxh.mapper.TbSpecificationMapper;
import com.dmxh.mapper.TbTypeTemplateMapper;
import com.dmxh.params.QueryParams;
import com.dmxh.pojo.*;
import com.dmxh.result.PageList;
import com.dmxh.service.TemplateService;
import com.dmxh.vo.BrandVo;
import com.dmxh.vo.SpecificationVo;
import com.dmxh.vo.Temp;
import com.dmxh.vo.TypeTemplateVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TemplateServiceImpl implements TemplateService {

    @Autowired
    private TbTypeTemplateMapper tbTypeTemplateMapper;
    @Autowired
    private TbBrandMapper tbBrandMapper;
    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;

    @Override
    public PageList<TypeTemplateVo> queryPage(QueryParams<TypeTemplateVo> queryParams) {
        Page<TbTypeTemplate> page = PageHelper.startPage(queryParams.getPageNum(), queryParams.getPageSize());
        String name = queryParams.getTrimParam().getName();
        TbTypeTemplateExample tbTypeTemplateExample = new TbTypeTemplateExample();
        if (!StrUtil.isEmpty(name)) {
            tbTypeTemplateExample.createCriteria().andNameLike("%"+name+"%");
        }
        List<TbTypeTemplate> tbTypeTemplates = tbTypeTemplateMapper.selectByExample(tbTypeTemplateExample);
        PageInfo<TbTypeTemplate> info = page.toPageInfo();
        List<TypeTemplateVo> collect = tbTypeTemplates.stream().map(tbTypeTemplate -> {
            StrBuilder brands = new StrBuilder();
            StrBuilder specs = new StrBuilder();
            StrBuilder b = new StrBuilder();
            TypeTemplateVo typeTemplateVo = new TypeTemplateVo();
            BeanUtils.copyProperties(tbTypeTemplate, typeTemplateVo);
            //转换品牌字符串格式
            List<BrandVo> brandVos = JSON.parseArray(typeTemplateVo.getBrandIds(), BrandVo.class);
            for (BrandVo brandVo : brandVos) {
                brands.append(brandVo.getText()).append(",");
            }
            typeTemplateVo.setBrandIds(brands.toString().substring(0,brands.toString().length()-1));
            //转换规格字符串格式
            List<SpecificationVo> specificationVos = JSON.parseArray(typeTemplateVo.getSpecIds(), SpecificationVo.class);
            for (SpecificationVo specificationVo : specificationVos) {
                specs.append(specificationVo.getText()).append(",");
            }
            typeTemplateVo.setSpecIds(specs.toString().substring(0,specs.toString().length()-1));
            //转换扩展属性字符串格式
            return typeTemplateVo;
        }).collect(Collectors.toList());

        return new PageList<>(info.getPageNum(), info.getPageSize(), info.getTotal(), collect);
    }

    @Override
    public TypeTemplateVo queryById(long id) {
        TbTypeTemplate tbTypeTemplate = tbTypeTemplateMapper.selectByPrimaryKey(id);
        TypeTemplateVo typeTemplateVo = new TypeTemplateVo();
        BeanUtils.copyProperties(tbTypeTemplate,typeTemplateVo);
        return typeTemplateVo;
    }

    @Override
    public void addInfo(TypeTemplateVo typeTemplateVo) {
        StrBuilder strBuilder1 = new StrBuilder().append("[");
        StrBuilder strBuilder2 = new StrBuilder().append("[");
        //将字符串转换为long类型的集合
        String brandIds = typeTemplateVo.getBrandIds().substring(1,typeTemplateVo.getBrandIds().length()-1);
        List<String> list = Arrays.asList(brandIds.split(","));
        List<Long> collect = list.stream().map(x->Long.valueOf(x)).collect(Collectors.toList());
        TbBrandExample tbBrandExample = new TbBrandExample();
        tbBrandExample.createCriteria().andIdIn(collect);
        List<TbBrand> tbBrands = tbBrandMapper.selectByExample(tbBrandExample);
        for (TbBrand tbBrand : tbBrands) {
            String s = JSON.toJSONString(new Temp(tbBrand.getId(), tbBrand.getName()));
            strBuilder1.append(s).append(",");
        }
        String substring1 = strBuilder1.toString().substring(0, strBuilder1.length() - 1);
        substring1 = substring1 + "]";
        //
        String specIds = typeTemplateVo.getSpecIds().substring(1,typeTemplateVo.getBrandIds().length()-1);
        List<String> list1 = Arrays.asList(specIds.split(","));
        List<Long> collect1 = list1.stream().map(Long::valueOf).collect(Collectors.toList());
        TbSpecificationExample tbSpecificationExample = new TbSpecificationExample();
        tbSpecificationExample.createCriteria().andIdIn(collect1);
        List<TbSpecification> tbSpecifications = tbSpecificationMapper.selectByExample(tbSpecificationExample);
        for (TbSpecification tbSpecification : tbSpecifications) {
            String s = JSON.toJSONString(new Temp(tbSpecification.getId(), tbSpecification.getSpecName()));
            strBuilder2.append(s).append(",");
        }
        String substring2 = strBuilder2.toString().substring(0, strBuilder2.length() - 1);
        substring2 = substring2 + "]";
        //
        String customAttributeItems = typeTemplateVo.getCustomAttributeItems();
        TbTypeTemplate tbTypeTemplate = new TbTypeTemplate(typeTemplateVo.getId(), typeTemplateVo.getName(), substring2, substring1, customAttributeItems);
        tbTypeTemplateMapper.insertSelective(tbTypeTemplate);
    }

    @Override
    public void updateInfo(TypeTemplateVo typeTemplateVo) {
        StrBuilder strBuilder1 = new StrBuilder().append("[");
        StrBuilder strBuilder2 = new StrBuilder().append("[");
        //将字符串转换为long类型的集合
        String brandIds = typeTemplateVo.getBrandIds().substring(1,typeTemplateVo.getBrandIds().length()-1);
        List<String> list = Arrays.asList(brandIds.split(","));
        List<Long> collect = list.stream().map(x->Long.valueOf(x)).collect(Collectors.toList());
        TbBrandExample tbBrandExample = new TbBrandExample();
        tbBrandExample.createCriteria().andIdIn(collect);
        List<TbBrand> tbBrands = tbBrandMapper.selectByExample(tbBrandExample);
        for (TbBrand tbBrand : tbBrands) {
            String s = JSON.toJSONString(new Temp(tbBrand.getId(), tbBrand.getName()));
            strBuilder1.append(s).append(",");
        }
        String substring1 = strBuilder1.toString().substring(0, strBuilder1.length() - 1);
        substring1 = substring1 + "]";
        //
        List<Long> list1 = JSON.parseArray(typeTemplateVo.getSpecIds(), Long.class);
        TbSpecificationExample tbSpecificationExample = new TbSpecificationExample();
        tbSpecificationExample.createCriteria().andIdIn(list1);
        List<TbSpecification> tbSpecifications = tbSpecificationMapper.selectByExample(tbSpecificationExample);
        for (TbSpecification tbSpecification : tbSpecifications) {
            String s = JSON.toJSONString(new Temp(tbSpecification.getId(), tbSpecification.getSpecName()));
            strBuilder2.append(s).append(",");
        }
        String substring2 = strBuilder2.toString().substring(0, strBuilder2.length() - 1);
        substring2 = substring2 + "]";
        //
        String customAttributeItems = typeTemplateVo.getCustomAttributeItems();
        TbTypeTemplate tbTypeTemplate = new TbTypeTemplate(typeTemplateVo.getId(), typeTemplateVo.getName(), substring2, substring1, customAttributeItems);
        tbTypeTemplateMapper.updateByPrimaryKeySelective(tbTypeTemplate);
    }

    @Override
    public void deleteInfo(long id) {
        tbTypeTemplateMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteBatch(Integer[] ids) {
        List<Integer> list = Arrays.asList(ids);
        List<Long> collect = list.stream().map(Long::valueOf).collect(Collectors.toList());
        TbTypeTemplateExample tbTypeTemplateExample = new TbTypeTemplateExample();
        tbTypeTemplateExample.createCriteria().andIdIn(collect);
        tbTypeTemplateMapper.deleteByExample(tbTypeTemplateExample);
    }

    @Override
    public List<TbTypeTemplate> queryAll() {
        return tbTypeTemplateMapper.selectByExample(null);
    }
}
